package gob.pe.inei.esvit.web.controller;

import gob.pe.inei.esvit.web.bean.LoginBean;
import gob.pe.inei.esvit.web.validator.LoginValidator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
@RequestMapping(value = "/login.htm")
public class LoginController {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());

	@Autowired
	private LoginValidator loginValidator;

	public void setUserValidation(LoginValidator loginValidator) {
		this.loginValidator = loginValidator;
	}

	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(LoginBean bean, BindingResult result) {
		loginValidator.validate(bean, result);
		if (result.hasErrors()) {
			return "login";
		}
		return "redirect:/frame.htm";
	}

	@RequestMapping(method = RequestMethod.GET)
	protected LoginBean formBackingObject(HttpServletRequest request)
			throws ServletException {
		LoginBean bean = new LoginBean();
		bean.setUsername("username");
		return bean;
	}
}
